<?php
function bin_select($arr,$value)
{

    $count = count($arr);
    $start = 0;
    $end = $count - 1;
    $mid = (int)floor(($start + $end)/2);
    while ($start<=$end){
        if($value<$arr[$mid]){
            $end = $mid-1;
            $mid = (int)floor(($start + $end)/2);
        }elseif ($value>$arr[$mid]){
            $start = $mid+1;
            $mid = (int)floor(($start + $end)/2);
        }else{
            return $mid;
        }
    }
    return -1;
}
//递归
function binSelect($arr,$val,$start,$end)
{
    if($start<= $end){
        $mid = (int)floor(($start+$end)/2);
        if($val<$arr[$mid]){
            return binSelect($arr,$val,$start,$mid-1);
        }elseif ($val>$arr[$mid]){
            return binSelect($arr,$val,$mid+1,$end);
        }else{
            return $mid;
        }
    }
    return -1;

}
$b = array(1,6,7,8,9);
var_dump(binSelect($b,9,0,count($b)-1));